package com.zhongyouyun.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.DeptModel;
import com.zhongyouyun.admin.model.RoleModel;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.admin.service.DeptService;
import com.zhongyouyun.admin.service.RoleService;
import com.zhongyouyun.other.model.PageRequest;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class QueryUtils {
    /**
     * 全部数据权限
     */
    public static final Integer DATA_SCOPE_ALL = 1;

    /**
     * 部门数据权限
     */
    public static final Integer DATA_SCOPE_DEPT = 2;

    /**
     * 部门及以下数据权限
     */
    public static final Integer DATA_SCOPE_DEPT_AND_CHILD = 3;

    /**
     * 仅本人数据权限
     */
    public static final Integer DATA_SCOPE_SELF = 4;

    public static void fiterQuery(QueryWrapper queryWrapper, PageRequest pageRequest) {
        queryWrapper.eq("1","1");
        pageRequest.getColumnFilters().forEach((k, v) -> {
            Map map = (Map) v;
            String eq = (String) map.get("eq");
            if ("%".equals(eq) && ObjectUtil.isNotEmpty(map.get("value"))) {
                queryWrapper.like(StrUtil.toUnderlineCase(k), map.get("value"));
            }else if("!%".equals(eq) && ObjectUtil.isNotEmpty(map.get("value"))){
                queryWrapper.notLike(StrUtil.toUnderlineCase(k), map.get("value"));
            }else if("=".equals(eq) && ObjectUtil.isNotEmpty(map.get("value"))){
                queryWrapper.eq(StrUtil.toUnderlineCase(k), map.get("value"));
            }else if("!=".equals(eq) && ObjectUtil.isNotEmpty(map.get("value"))){
                queryWrapper.ge(StrUtil.toUnderlineCase(k), map.get("value"));
            }
        });
    }

    public static List<Long> findChaDeptIds(UserModel userModel) {
        DeptService deptService = (DeptService) SpringUtil.getBean(DeptService.class);//部门处理类
        RoleService roleService = (RoleService) SpringUtil.getBean(RoleService.class);//角色处理类
        List<Long> idsDeptList = new ArrayList<Long>();
        if (ObjectUtil.isEmpty(userModel.getRoleId())) {
            return idsDeptList;
        } else {

            if (ObjectUtil.isNotEmpty(userModel.getRoleId())) {
                //添加角色数据
                QueryWrapper roleModelQueryWrapper = new QueryWrapper<RoleModel>();
                roleModelQueryWrapper.in("id", LongUtil.Longva(userModel.getRoleId().split(",")));
                List<RoleModel> roleModelList = roleService.selectList(roleModelQueryWrapper);
                userModel.setRoleModelList(roleModelList);
            }
            List<RoleModel> roleModelList = userModel.getRoleModelList();
            //集团管理员
            if (ObjectUtil.isNotEmpty(userModel.getDeptId())) {
                //添加部门数据
                QueryWrapper deptModelQueryWrapper = new QueryWrapper<DeptModel>();
                deptModelQueryWrapper.eq("id", userModel.getDeptId());
                List<DeptModel> deptModelList = deptService.selectList(deptModelQueryWrapper);
                deptModelList.forEach(deptModel -> {
                    String chihids = deptService.getchild(deptModel.getId());
                    deptModel.setFullPathId(chihids);
                });
                userModel.setDeptModelList(deptModelList);
            }
            List<DeptModel> deptModelList = userModel.getDeptModelList();
            for (RoleModel roleModel : roleModelList) {
                if ("0".equals(roleModel.getType())) {
                    return idsDeptList;
                }
            }
            for (DeptModel deptModel : deptModelList) {
                if (DATA_SCOPE_ALL.equals(deptModel.getDataScope())) {
                    return idsDeptList;
                } else if (DATA_SCOPE_DEPT.equals(deptModel.getDataScope())) {
                    idsDeptList.add(deptModel.getId());
                } else if (DATA_SCOPE_DEPT_AND_CHILD.equals(deptModel.getDataScope())) {
                    List<Long> departIds = getChildDepartIdList(deptModel);
                    if (!CollectionUtils.isEmpty(departIds)) {
                        departIds.forEach(id -> {
                            idsDeptList.add(id);
                        });
                    }
                } else if (DATA_SCOPE_SELF.equals(deptModel.getDataScope())) {
                    return idsDeptList;
                }
            }
        }
        return idsDeptList;
    }

    /**
     * 获取本部门和子部门ID列表
     *
     * @param deptModel 部门信息
     * @return 本部门和子部门ID列表
     */
    private static List<Long> getChildDepartIdList(DeptModel deptModel) {
        String PIdStr = deptModel.getFullPathId();
        PIdStr = PIdStr.replace("$,", "").replace(",0", "");
        String[] childArr = PIdStr.split(",");
        return LongUtil.Longva(childArr);
    }
}
